package cn.yunhe.server;

import cn.yunhe.dao.ITopicDao;
import cn.yunhe.entity.Blank;
import cn.yunhe.entity.Reply;
import cn.yunhe.entity.Topic;
import cn.yunhe.util.PageLimitUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TopicServer {

    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    ITopicDao mtopicDao = session.getMapper(ITopicDao.class);

    // 发帖方法
    public boolean PostTopic(Topic topic) {
        int num = mtopicDao.PostTopic(topic); //定义变量接收发帖方法执行结果
        if (num > 0) {
            session.commit();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询所有主贴内容方法
     *
     * @return 主贴对象集合
     */
    public List<Topic> SelectAllTopic(Blank blank, int index) {
        int size = PageLimitUtil.getPAGESIZE();
        Map<String, Object> map = new HashMap<String, Object>();
        int newindex = (index - 1) * size;
        map.put("blank", blank);
        map.put("index", newindex);
        map.put("size", size);
        return mtopicDao.SelectAllTopic(map);
    }

    /**
     * 通过关键字查询获取贴子标题方法
     *
     * @param keyword 查询关键字
     * @return 所有相关帖子标题集合字符串
     */
    public String SelectTopicByKeyword(String keyword) {
        List<String> list = mtopicDao.SelectTopicByKeyword(keyword);
        StringBuilder titles = new StringBuilder(); //新建Stringbuilder对象

        for (String str : list) {
            titles.append(str + ","); //使用StringBuilder拼接字符串
        }
        return titles.toString();
    }

    /**
     * 通过帖子对象和板块对象精确查询对应的帖子
     *
     * @param topic
     * @return
     */
    public List<Topic> AccurateSelectTopicByTopic(Topic topic, Blank blank) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topic", topic);
        map.put("blank", blank);

        List<Topic> list =  mtopicDao.AccurateSelectTopicByTopic(map);
        return list;
    }

    /**
     * 通过板块编号和帖子标题精确查询帖子方法
     *
     * @param title
     * @param bId
     * @return
     */
    public String AccurateSelectTopicByTitleBId(String title, int bId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", title);
        map.put("bId", bId);
        List<String> list = mtopicDao.AccurateSelectTopicByTitleBId(map);
        StringBuilder titles = new StringBuilder(); //新建Stringbuilder对象
        for (String str : list) {
            titles.append(str + ","); //使用StringBuilder拼接字符串
        }
        return titles.toString();
    }

    /**
     * 统计每个板块最大条数
     *
     * @param blank
     * @return
     */
    public int count(Blank blank) {
        return mtopicDao.count(blank);
    }

    public int countBykeyword(String keyword){
        return mtopicDao.countBykeyword(keyword);
    }

    /**
     * 帖子删除方法,
     * @param topic 主贴对象
     * @return
     */
    public boolean TopicDelete(Topic topic) {
        int flagTopic = mtopicDao.DeletTopic(topic); //执行主贴删除方法
        int pId = topic.getpId(); //获取主贴ID
       /* Reply reply = new Reply(); //新建回帖对象
        reply.setTopic(topic); //传入主贴对象
        int flagReply = mtopicDao.DeletReply(reply); //执行回帖删除方法*/
        if (flagTopic > 0){
            session.commit(); //如果主贴和回帖均删除成功则提交事务
            return true;
        }else {
            return false;
        }
    }

    /**
     *通过关键字查询相关主贴
     * @param keyword
     * @return
     */
    public List<Topic> FuzzySelectTopicByKeyword(String keyword){
        return mtopicDao.FuzzySelectTopicByKeyword(keyword);
    }

    /**
     * 通过关键字,当前页,总页数查询主贴
     * @param keyword 关键字
     * @param currPage 当前页数
     * @param totalPage 总页数
     * @return 主贴对象
     */
    public List<Topic> FuzzySelectTopicByKeywordCurrPageServlet(String keyword,int currPage,int totalPage){
        //建立map对象封装参数
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("keyword",keyword);
        map.put("currPage",currPage); //插入页面索引
        map.put("totalPage",totalPage); //插入页面尺寸
        return mtopicDao.FuzzySelectTopicByKeywordCurrPage(map);
    }


}
